package com.xidian.zhiancloud.service.ledgerManager.relatedPart;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.RelatedPartyDocMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.relatedParty.RelatedPartyDocQO;
import com.xidian.zhiancloud.vo.ledgerManagerment.relatedParty.RelatedPartyDocVO;
import com.xidian.zhiancloud.model.RelatedPartyDoc;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:16
 */
@Service
public class RelatedPartyDocService extends ServiceImpl<RelatedPartyDocMapper, RelatedPartyDoc> implements IService<RelatedPartyDoc> {

    @Autowired
    private RelatedPartyDocMapper relatedPartyDocMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<RelatedPartyDocVO> getListByQO(RelatedPartyDocQO relatedPartyDocQO) {
        Page<RelatedPartyDocVO> page = new Page<>(relatedPartyDocQO.getCurrent(), relatedPartyDocQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        return relatedPartyDocMapper.getListByQO(page,relatedPartyDocQO);
    }

    public List<RelatedPartyDocVO> getListByIds(Long[] ids) {
        List<RelatedPartyDoc> res = relatedPartyDocMapper.selectBatchIds(Arrays.asList(ids));
        List<RelatedPartyDocVO> ans = new LinkedList<>();
        res.forEach(t -> {
            RelatedPartyDocVO vo = new RelatedPartyDocVO();
            BeanUtils.copyProperties(t, vo);
            SysUser user = sysUserMapper.selectById(vo.getCreatorId());
            vo.setCreatorName(user == null ? "该用户已被删除" : user.getName());
            ans.add(vo);
        });
        return ans;
    }

    public Map<Long, String> getList(Long companyId) {
        LambdaQueryWrapper<RelatedPartyDoc> query = Wrappers.<RelatedPartyDoc>lambdaQuery();
        query.eq(RelatedPartyDoc::getCompanyId, companyId);
        List<RelatedPartyDoc> list = relatedPartyDocMapper.selectList(query);
        Map<Long,String> ans = new HashMap<>();
        for (RelatedPartyDoc relatedPartyDoc : list) {
            if(relatedPartyDoc.getId()!=null){
                ans.put(relatedPartyDoc.getId(),relatedPartyDoc.getName()==null?"该第三方没有名称":relatedPartyDoc.getName());
            }
        }
        return ans;
    }
}
